package com.bootlever.framework.zookeeper.templates;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.NodeCache;
import org.apache.curator.framework.recipes.cache.PathChildrenCache;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;

import java.util.List;

public class ZookeeperTemplate {

    private CuratorFramework curatorFramework;

    public ZookeeperTemplate(CuratorFramework curatorFramework){
        this.curatorFramework = curatorFramework;
    }

    /**
     * 创建节点
     *
     * @param path
     * @return
     * @throws Exception
     */
    public NodeCache createNodeCache(String path) throws Exception {
        NodeCache nodeCache = new NodeCache(curatorFramework, path, false);
        nodeCache.start(true);
        return nodeCache;
    }

    public PathChildrenCache createPathChildrenCache(String path) throws Exception {
        PathChildrenCache childrenCache = new PathChildrenCache(curatorFramework,path, true);
        childrenCache.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT);
        return childrenCache;
    }

    public String createPath(String path) throws Exception {
        return curatorFramework.create().forPath(path);
    }

    public String createPath(String path, CreateMode mode) throws Exception {
        return curatorFramework.create().withMode(mode).forPath(path);
    }

    public String createPath(String path, String data) throws Exception {
        return curatorFramework.create().forPath(path, data.getBytes());
    }

    public String createPathWithParent(String path, String data, CreateMode mode) throws Exception{
        return curatorFramework.create().creatingParentsIfNeeded().withMode(mode).forPath(path, data.getBytes());
    }

    public String createPathWithParent(String path) throws Exception {
        return curatorFramework.create().creatingParentsIfNeeded().forPath(path);
    }

    public String createPathWithParent(String path, CreateMode mode) throws Exception {
        return curatorFramework.create().creatingParentsIfNeeded().withMode(mode).forPath(path);
    }

    public String createPathWithParent(String path, String data) throws Exception {
        return curatorFramework.create().creatingParentsIfNeeded().forPath(path, data.getBytes());
    }

    public String createPath(String path, String data, CreateMode mode) throws Exception{
        return curatorFramework.create().withMode(mode).forPath(path, data.getBytes());
    }

    public Stat setData(String path, String data) throws Exception {
        return curatorFramework.setData().forPath(path, data.getBytes());
    }

    public String getData(String path) throws Exception {
        return new String(curatorFramework.getData().forPath(path));
    }

    public void delete(String path) throws Exception {
        curatorFramework.delete().forPath(path);
    }

    public List<String> getChildren(String path) throws Exception {
        return curatorFramework.getChildren().forPath(path);
    }
}